package com.hakgc.rent.service.impl;

import com.hakgc.rent.dao.UserDao;
import com.hakgc.rent.dao.impl.UserDaoImpl;
import com.hakgc.rent.entity.User;
import com.hakgc.rent.exception.LoginException;
import com.hakgc.rent.service.UserService;

import java.util.List;
import java.util.Objects;

/**
 * @author Xing.Jiejian
 * @date 2020/11/17
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public User login(String name, String password) throws LoginException {
        // 通过name查询数据库是否包含相关对象
        User user = userDao.findByName(name);
        // 查不到对应用户返回异常
        if (user==null){
            throw new LoginException("用户名错误，不存在该用户！");
        }
        // 比较密码
        if (!Objects.equals(password,user.getPassword())){
            throw new LoginException("密码错误，请重新尝试！");
        }
        return user;
    }

    @Override
    public boolean add(User user) {
        return userDao.save(user);
    }

    @Override
    public List<User> findAll(String name) {
        return userDao.findAll(name);
    }
}
